<?php

namespace Admin\Controller;

use Think\Controller;

class LinkController extends CommonController
{
    public function lst()
    {
        $link=D('Link');
        $res=$link->query("SELECT * FROM link");
        $this->assign('res',$res);
        $this->display();
    }

    public function add()//添加
    {
        $link=D('Link');
        if (IS_POST){
            $data['url_name']=I('post.url_name');
            $data['url']=I('post.url');
            $data['desc']=I('post.desc');
            if ($link->create($data)){
                if ($link->add()){
                    $this->success('添加成功',U('lst'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($link->getError());
            }
            return;
        }

        $this->display();
    }
    public function edit()//修改
    {
        $link=D('Link');
       $id=I('get.id');
      $res=$link->query("SELECT * FROM link WHERE id=$id");
//        $res=$link->find(I('id'));
        $this->assign('res',$res);
        if (IS_POST) {
            $data['url_name'] = I('post.url_name');
            $data['url'] = I('post.url');
            $data['desc'] = I('post.desc');
            $data['id'] = I('post.id');
            if ($link->create()) {
                if ($link->save($data)) {
                    $this->success('修改成功！', U('lst'));
                } else {
                    $this->error('修改失败！',U('lst'));
                }
            } else {
                $this->error($link->getError());
            }
            return;
        }
        $this->display();
    }
    public function del()//删除
    {
        $lnik = M("link"); // 实例化User对象
        if ($lnik->delete(I('id'))) {
            $this->success('删除成功！', U('lst'));
        } else {
            $this->error('删除失败！');
        }

    }

}

